package com.xczl.demo.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xczl.demo.dao.GoodsDao;
import com.xczl.demo.dao.OrderDao;
import com.xczl.demo.domain.Cart;
import com.xczl.demo.domain.Order;
import com.xczl.demo.domain.OrderDetail;
import com.xczl.demo.domain.User;

/**
 * 订单服务
 * 
 * @author DMJ
 *
 */
@Service
@Transactional
public class OrderService {

	@Autowired
	private OrderDao orderDao;

	@Autowired
	private GoodsDao goodsDao;

	/**
	 * 创建订单
	 * 
	 * @param user
	 * @param list
	 */
	public void createOrder(User user, List<Cart> cartList) {
		Order order = new Order();
		order.setCreateTime(new Date());
		order.setState(Order.STATE_UNPAID);
		order.setUserId(user.getUserId());
		BigDecimal total = BigDecimal.valueOf(0.00);
		List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();
		OrderDetail detail = null;
		for (Cart temp : cartList) {
			detail = new OrderDetail();
			detail.setGoodsId(temp.getGoods().getGoodsId());
			detail.setName(temp.getGoods().getName());
			detail.setOrderId(order.getOrderId());
			detail.setPrice(temp.getGoods().getPrice());
			detail.setQuantity(temp.getQuantity());
			orderDetailList.add(detail);
			total = total.add(temp.getGoods().getPrice());
		}
	}
}
